#!/bin/bash
version=$(date -d "8 hour" -u +%y.%m.%d.%H)
cd ~/app-root/data/github-sh/xpi/pan
latest=$(curl -s https://addons.mozilla.org/zh-CN/firefox/addon/pan/versions/ |grep '\.xpi'|awk -F '\"' '{print $2}'|sed '2,$d')
if ! grep -q "$latest" ~/app-root/data/temp/xpi/pan/latest.txt; then
echo "$latest" > ~/app-root/data/temp/xpi/pan/latest.txt
wget ${latest} --output-document=pan-${version}.xpi
unzip -o pan-${version}.xpi bootstrap.js chrome/locale/en-US/* chrome/locale/zh-CN/* -d do/
zip -d -r pan-${version}.xpi META-INF/* chrome/locale/* chrome/content/ui/skin/*
mv pan-${version}.xpi do/pan-${version}.xpi
cd do/
sed -i '/prefs\.json/s#s://adblockplus.org/subscriptions2.xml#://halflife.oschina.io/index/subscriptions2.xml#' bootstrap.js
sed -i '/prefs\.json/s#\"subscriptions_fall[a-zA-Z0-9\ \?\%\=\_\"\&\:\.\/\-]\{1,\}, ##g' bootstrap.js
sed -i '/prefs\.json/s#patternsbackups\": [0-9]#patternsbackups\": 0#' bootstrap.js
sed -i '/prefs\.json/s#proxyMode\": \"auto#proxyMode\": \"disabled#' bootstrap.js
sed -i '/prefs\.json/s#savestats\": false#savestats\": true#' bootstrap.js
sed -i '/prefs\.json/s#enableStatistics\": false#enableStatistics\": true#' bootstrap.js
#sed -i 's#s://adblockplus.org/subscriptions2.xml#://halflife.oschina.io/index/subscriptions2.xml#g' defaults/prefs.json
#sed -i -e '/enableStatistics/s#false#true#' -e '/savestats/s#false#true#' -e '/patternsbackups/s#5#0#' defaults/prefs.json
#sed -i -e '/subscriptions_fall/d' -e '/proxyMode/s#auto#disabled#' defaults/prefs.json
sed -i "s#[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}#${version}#" install.rdf
zip -r pan-${version}.xpi *
rm -rf chrome/locale/ defaults/
rm -f ~/app-root/runtime/repo/扩展/$(ls ~/app-root/runtime/repo/扩展/|grep "Pan2-[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}.xpi")
mv -f pan-${version}.xpi ~/app-root/runtime/repo/扩展/Pan2-${version}.xpi
~/app-root/data/github-sh/log/log.sh "Pan2-${version}.xpi 更新"
fi
